package com.tjec.project.businessWorkbench.controller;

import java.util.List;

import com.tjec.framework.interceptor.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.LoginUser;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.businessWorkbench.domain.BmsBusinessPickup;
import com.tjec.project.businessWorkbench.service.IBmsBusinessPickupService;
import com.tjec.project.businessWorkbench.service.IBmsImportExcelService;
import com.tjec.project.system.domain.SysUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.BusinessExcelUtil;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * 收资管理Controller
 * 
 * @author pangyognfeng
 * @date 2020-04-24
 */
@Api(value = "收资管理", tags = { "收资管理接口" })
@RestController
@RequestMapping("/businessWorkbench/pickup")
public class BmsBusinessPickupController extends BaseController {
	@Autowired
	private IBmsBusinessPickupService bmsBusinessPickupService;
	@Autowired
	private IBmsImportExcelService bmsImportExcelService;
	@Autowired
	private TokenService tokenService;

	/**
	 * 查询收资管理列表
	 */
	@ApiOperation("查询收资管理列表")
	@PreAuthorize("@ss.hasPermi('businessWorkbench:pickup:list')")
	@GetMapping("/list")
	public TableDataInfo list(BmsBusinessPickup bmsBusinessPickup) {
		startPage();
		// Integer total =
		// bmsBusinessPickupService.selectBmsBusinessPickupListTotal(bmsBusinessPickup);
		List<BmsBusinessPickup> list = bmsBusinessPickupService.selectBmsBusinessPickupList(bmsBusinessPickup);
		return getDataTable(list);
		/*
		 * if(total>0) { return getDataTableNew(list,total); }else{ return
		 * getDataTable(list); }
		 */
	}

	/**
	 * 导出收资管理列表
	 */
	@ApiOperation("导出收资管理列表")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:pickup:export')")
	@Log(title = "收资管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BmsBusinessPickup bmsBusinessPickup) {
		List<BmsBusinessPickup> list = bmsBusinessPickupService.selectBmsBusinessPickupList(bmsBusinessPickup);
		BusinessExcelUtil<BmsBusinessPickup> util = new BusinessExcelUtil<BmsBusinessPickup>(BmsBusinessPickup.class);
		return util.exportExcel(list, "收资管理");
	}

	/**
	 * 获取收资管理详细信息
	 */
	@ApiOperation("获取收资管理详细信息")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:pickup:query')")
	@GetMapping(value = "/{guid}")
	public AjaxResult getInfo(@PathVariable("guid") String guid) {
		return AjaxResult.success(bmsBusinessPickupService.selectBmsBusinessPickupByGuid(guid));
	}

	/**
	 * 获取收资管理详细信息
	 */
	@ApiOperation("获取收资管理集合-用户资料归档")
	@GetMapping(value = "getPickups/{businessId}")
	public AjaxResult getPickups(@PathVariable("businessId") String businessId) {
		return AjaxResult.success(bmsBusinessPickupService.getPickupByGuid(businessId));
	}

	/**
	 * 新增收资管理
	 * 
	 * @throws Exception
	 */
	@ApiOperation("新增收资管理")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:pickup:add')")
	@Log(title = "收资管理", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping
	public AjaxResult add(@RequestBody BmsBusinessPickup bmsBusinessPickup) throws Exception {
		return toAjax(bmsBusinessPickupService.insertBmsBusinessPickup(bmsBusinessPickup));
	}

	/**
	 * 修改收资管理
	 * 
	 * @throws Exception
	 */
	@ApiOperation("修改收资管理")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:pickup:edit')")
	@Log(title = "收资管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BmsBusinessPickup bmsBusinessPickup) throws Exception {
		return toAjax(bmsBusinessPickupService.updateBmsBusinessPickup(bmsBusinessPickup));
	}

	/**
	 * 删除收资管理
	 * 
	 * @throws Exception
	 */
	@ApiOperation("批量删除收资管理")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:pickup:remove')")
	@Log(title = "收资管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
	public AjaxResult remove(@PathVariable String[] guids) throws Exception {
		return toAjax(bmsBusinessPickupService.deleteBmsBusinessPickupByGuids(guids));
	}

	/**
	 * 导入收资管理模板
	 * 
	 * @throws Exception
	 */
	@ApiOperation("导入收资管理模板")
	@GetMapping("/{guid}/{businessId}")
	public AjaxResult importPickupTemplate(@PathVariable("guid") String guid,
			@PathVariable("businessId") String businessId) throws Exception {
		return toAjax(bmsImportExcelService.importTemplte(guid, "2", businessId));
	}

	@ApiOperation("收资管理导入清单")
	@Log(title = "收资管理", businessType = BusinessType.IMPORT)
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, String businessId) throws Exception {
		BusinessExcelUtil<BmsBusinessPickup> util = new BusinessExcelUtil<BmsBusinessPickup>(BmsBusinessPickup.class);
		List<BmsBusinessPickup> pickupList = null;
		try {
			pickupList = util.importExcel(file.getInputStream());
		} catch (Exception e) {
			return AjaxResult.error("请导入本系统导出的自制模板文件！", 100);
		}

		if (pickupList == null) {
			return AjaxResult.error("请导入本系统导出的自制模板文件！", 100);
		} else if (pickupList.size() == 0) {
			return AjaxResult.error("导入文件为空文件！", 100);
		} else {
			for (BmsBusinessPickup bmsBusinessPickup : pickupList) {
				bmsBusinessPickup
						.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
				bmsBusinessPickup.setBusinessId(businessId);
			}
			int res = bmsBusinessPickupService.importBmsBusinessPickups(pickupList, businessId);
			return AjaxResult.success(res);
		}
	}
}
